/********************************************************************************
 *    Copyright (C) 2019 Quark Matter Research Center, IMP,CAS			*
 *                                                                              *
 *              This software is distributed under the terms of the             *
 *              GNU Lesser General Public Licence (LGPL) version 3,             *
 *                  copied verbatim in the file "LICENSE"                       *
 ********************************************************************************/
// in root all sizes are given in cm




#include "TSystem.h"
#include "TGeoManager.h"
#include "TGeoVolume.h"
#include "TGeoMaterial.h"
#include "TGeoMedium.h"
#include "TGeoPgon.h"
#include "TGeoMatrix.h"
#include "TGeoCompositeShape.h"
#include "TFile.h"
#include "TString.h"
#include "TList.h"
#include "TROOT.h"



#include <iostream>

using namespace TMath;



// some global variables
TGeoManager* gGeoMan = NULL;  // Pointer to TGeoManager instance


void Create_Magnet()
{
	  // -------   Load media from media file   -----------------------------------
	FairGeoLoader*    geoLoad = new FairGeoLoader("TGeo","FairGeoLoader");
	FairGeoInterface* geoFace = geoLoad->getGeoInterface();
	TString geoPath = gSystem->Getenv("VMCWORKDIR");
	cout<<"VMCWORKDIR is: "<<geoPath<<endl;
	TString medFile = geoPath + "/geometry/media.geo";
	// TString medFile = geoPath + "/geometry/media_etf.geo";
	cout<<"media file is: "<<medFile<<endl;
	geoFace->setMediaFile(medFile);
	geoFace->readMedia();
	//Get and create the required materials which defined in the global media.geo file
	FairGeoMedia*   geoMedia = geoFace->getMedia();
	FairGeoBuilder* geoBuild = geoLoad->getGeoBuilder();
	FairGeoMedium* mFe       = geoMedia->getMedium("iron");
	FairGeoMedium* mAir= geoMedia->getMedium("air");
	FairGeoMedium* mAl= geoMedia->getMedium("aluminium");
	//check if all media are found
	geoBuild->createMedium(mFe);
	geoBuild->createMedium(mAir);
	geoBuild->createMedium(mAl);
	// -----------------------------------------------------------------------------
	

	
	// -------   Output Geometry file name  ----------------------------------------
	const TString detectorName= "magnet";
	const TString versionName = "_simple";
	// const TString FileName =detectorName + versionName + ".root";
	const TString FileName =geoPath+"/geometry/"+detectorName + versionName + ".root";
	const TString fname=detectorName + versionName;
	// -----------------------------------------------------------------------------


	// -------     Create geometry and top volume     ------------------------------
	gGeoMan=(TGeoManager*)gROOT->FindObject("FAIRGeom");
	gGeoMan->SetName("Magnetgeom");
	gGeoMan->SetVisLevel(7);
	TGeoVolume* top= new TGeoVolumeAssembly("TOP");
	gGeoMan->SetTopVolume(top);
	
	TGeoMedium* pMedFe= gGeoMan->GetMedium("iron");
	  if ( ! pMedFe ) Fatal("Main", "Medium iron not found");
	TGeoMedium* pMedAir = gGeoMan->GetMedium("air");
	  if ( ! pMedAir ) Fatal("Main", "Medium air not found");
	
	TGeoVolume* magnet= new TGeoVolumeAssembly(fname);
	top->AddNode(magnet, 1);

		// magnet yoke
		TGeoBBox *magyoke1 = new TGeoBBox("magyoke1", 80, 50, 50);
		TGeoBBox *magyoke2 = new TGeoBBox("magyoke2", 40, 14, 51);

		TGeoCompositeShape *magyokec = new TGeoCompositeShape("magyokec", "magyoke1-magyoke2");
		TGeoVolume *magyoke = new TGeoVolume("magyoke", magyokec, pMedFe);
		magyoke->SetLineColor(kBlue);
		//magyoke->SetTransparency(50);
		magnet->AddNode(magyoke, 1, new TGeoTranslation(0, 0, 0));
				
	cout<<"Voxelizing."<<endl;
	top->Voxelize("");
	gGeoMan->CloseGeometry();
	magyoke->Draw();
	gGeoMan->CheckOverlaps(0.001);
	gGeoMan->PrintOverlaps();
	gGeoMan->Test();

	TFile* outfile = TFile::Open(FileName,"RECREATE");
	top->Write();
	outfile->Close();

}

